package
{
	/**
	 *
	 *@author CrazyChen 
	 *2013-4-10
	 */	
	import fl.controls.Button;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	public class EditPlantPanel extends Sprite
	{
		private static var instance:EditPlantPanel;
		
		private var x_input:TextInput;
		
		private var y_input:TextInput;
		
		private var w_input:TextInput;
		
		private var h_input:TextInput;
		
		private var close:SimpleButton;
		
		private var are_txt:TextArea;
		
		private var hp:TextInput;
		
		private var energy:TextInput;
		
		private var save:Button;
		
		public var targetPlant:PlantItem;
		public function EditPlantPanel()
		{
			super();
			if(instance) throw new Error("Singerleton Class!");
			this.x = TowerWarMap.instance.WIDTH - 183;
			this.y = TowerWarMap.instance.HEIGHT - 100;
			initface();
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
		}
		
		private function mouseDown(e:MouseEvent):void{
			this.startDrag(false,new Rectangle(0,0,TowerWarMap.instance.WIDTH-183,TowerWarMap.instance.HEIGHT - 100));
		}
		
		private function mouseUp(e:MouseEvent):void{
			this.stopDrag();
		}
		
		private function initface():void
		{
			var edit:Edit = new Edit();
			this.addChild(edit);
			x_input = edit.x_txt;
			y_input = edit.y_txt;
			w_input = edit.w_txt;
			h_input = edit.h_txt;
			energy = edit.energy;
			hp = edit.hp;
			close = edit.close;
			are_txt = edit.print_txt;
			close.addEventListener(MouseEvent.CLICK,hide);
            save = edit.save;
			save.addEventListener(MouseEvent.CLICK,saveData);
		}
		
		protected function saveData(event:MouseEvent):void
		{
			if(x_input.text != "")targetPlant.x = int(x_input.text);

			if(y_input.text != "")targetPlant.y = int(y_input.text);
		
			if(w_input.text != "")targetPlant.bmp.width = int(w_input.text);
			
			if(h_input.text != "")targetPlant.bmp.height = int(h_input.text);
			
			if(hp.text != "")targetPlant.hp = int(hp.text);
			
			if(energy.text != "")targetPlant.energy = int(energy.text);
			
			var ary:Array = targetPlant.gradAry;
			var str:String="";
			for(var i:int=0;i<ary.length;i++){
				var p:Point = ary[i];
				str +="("+p.x+","+p.y+"),"
			}
			str.slice(0,str.length-1);
			are_txt.text = str;
			targetPlant.refreshBmpPosition();
		}
		
		public static function getInstance():EditPlantPanel{
			return instance ||=new EditPlantPanel();
		}
		
		public function show(plant:PlantItem):void{
			targetPlant = plant;
			TowerWarMap.instance.addChild(this);
			x_input.text = targetPlant.x + "";
			y_input.text = targetPlant.y + "";
			w_input.text = targetPlant.width + "";
			h_input.text = targetPlant.height + "";
			hp.text = targetPlant.hp+"";
			energy.text = targetPlant.energy+"";
			var ary:Array = targetPlant.gradAry;
			var str:String="";
			for(var i:int=0;i<ary.length;i++){
				var p:Point = ary[i];
			str +="("+p.x+","+p.y+"),"
			}
			str.slice(0,str.length-1);
			are_txt.text = str;
		}
		
		private function hide(e:MouseEvent):void{
			TowerWarMap.instance.removeChild(this);
			targetPlant = null;
		}
	}
}